package com.saifan.wyy_ov.ui.property;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.DecorateInfoBean;
import com.saifan.wyy_ov.data.bean.RequestBean;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.LabelView;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.DensityUtil;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-5-13 15:56:47
 * @descrption 装修申请记录
 */

public class DecorationApplyRecordActivity extends BaseActivity {
    private Toolbar toolbar;
    private ListView listView;
    private List<DecorateInfoBean> decorateInfoBeans = new ArrayList<>();
    private CommonAdapter<DecorateInfoBean> adapter;
    private View loadlay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();
        initAdapter();

    }




    private void initAdapter() {
        adapter = new CommonAdapter<DecorateInfoBean>(this, decorateInfoBeans, R.layout.decoration_apply_record_item) {
            @Override
            public void convert(ViewHolderHelper helper, DecorateInfoBean item) {

                helper.setText(R.id.YZXM, StringUtil.returnShow(item.getZXSQ_YZXM()));
                if (item.getZXSQ_ZXLX() == 0) {
                    helper.setText(R.id.ZXLX, "家装");
                } else {
                    helper.setText(R.id.ZXLX, "公装");
                }
                helper.setText(R.id.ZXGS, StringUtil.returnShow(item.getZXSQ_ZXGS()));
                helper.setText(R.id.ZXFZR, StringUtil.returnShow(item.getZXSQ_ZXFZR()) + "    " + StringUtil.returnShow(item.getZXSQ_ZXGSDH()));
                String beginTime = "";
                String endTime = "";
                if (!StringUtil.isNullOrEmpty(item.getZXSQ_QSSJ()) && item.getZXSQ_QSSJ().length() > 9) {
                    beginTime = item.getZXSQ_QSSJ().substring(0, 10);
                }
                if (!StringUtil.isNullOrEmpty(item.getZXSQ_JZSJ()) && item.getZXSQ_JZSJ().length() > 9) {
                    endTime = item.getZXSQ_JZSJ().substring(0, 10);
                }
                helper.setText(R.id.SJ, beginTime + "至" + endTime);
                helper.setText(R.id.YZXM, StringUtil.returnShow(item.getZXSQ_YZXM()));
                LinearLayout targetView = helper.getView(R.id.layout);

                int window_width_dp = getWindowManager().getDefaultDisplay().getWidth();
                int window_width_px = DensityUtil.px2dip(
                        DecorationApplyRecordActivity.this, window_width_dp) - 20;


                LabelView label = helper.getView(12345);


                if (label == null) {
                    label = new LabelView(DecorationApplyRecordActivity.this);
                    label.setId(12345);

                    label.setHeight(DensityUtil.dip2px(DecorationApplyRecordActivity.this, 40));
                    label.setWidth(DensityUtil.dip2px(DecorationApplyRecordActivity.this, 120));
                    label.setGravity(Gravity.BOTTOM | Gravity.CENTER);

                }
                if (!item.isZXSQ_SQSL() && !StringUtil.isNullOrEmpty(item.getZXSQ_BSLYY())) {
                    label.setBackgroundColor(getResources().getColor(R.color.lable1));
                    label.setTargetViewInBaseAdapter(targetView, window_width_px, 0, LabelView.Gravity.RIGHT_TOP);
                    label.setText("未受理");
                    helper.getView(R.id.BSLYY).setVisibility(View.VISIBLE);
                    helper.setText(R.id.BSLYY, "未受理原因：" + StringUtil.returnShow(item.getZXSQ_BSLYY()));

                } else if (!item.isZXSQ_SQSL() && StringUtil.isNullOrEmpty(item.getZXSQ_BSLYY())) {
                    label.setBackgroundColor(getResources().getColor(R.color.lable2));
                    label.setTargetViewInBaseAdapter(targetView, window_width_px, 0, LabelView.Gravity.RIGHT_TOP);
                    label.setText("审核中");
                    helper.getView(R.id.BSLYY).setVisibility(View.GONE);
                } else if (item.isZXSQ_SQSL() ) {
                    label.setBackgroundColor(getResources().getColor(R.color.lable3));
                    label.setTargetViewInBaseAdapter(targetView, window_width_px, 0, LabelView.Gravity.RIGHT_TOP);
                    label.setText("已受理");
                    helper.getView(R.id.BSLYY).setVisibility(View.GONE);
                }


            }
        };
        listView.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        query();
    }

    private void query() {
        RequestBean requestBean = new RequestBean();
        requestBean.setZXSQ_FKGXWJ(getRoomInfoBean().getFKGX_ZJ());

        loadlay.setVisibility(View.VISIBLE);
        GetData getData = new GetData();
        getData.getDataJson(this, Api.Decorate.DECORATE_INFO, requestBean, "", new ResponseData() {
            @Override
            public void onSuccess(String json) {
                List<DecorateInfoBean> list = new Gson().fromJson(json, new TypeToken<List<DecorateInfoBean>>() {
                }.getType());
                if (list!=null&&!list.isEmpty()) {
                    decorateInfoBeans = list;
                    adapter.mDatas = decorateInfoBeans;
                    adapter.notifyDataSetChanged();

                }else {

                    ToastUtil.showToast(DecorationApplyRecordActivity.this,"暂无相关信息");

                }
                loadlay.setVisibility(View.GONE);

            }

            @Override
            public void onFailed(String str) {
                loadlay.setVisibility(View.GONE);
            }
        });
    }

    public void setContentView() {
        setContentView(R.layout.activity_decoration_apply_record);
        this.listView = (ListView) findViewById(R.id.listView);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
          loadlay = findViewById(R.id.loading_lay);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }




}
